package self;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.Set;

/**
 * Created by 刘万祥 on 2017/5/5 0005.
 */
public class ZremrangeByRankTest {
    public static void main(String[] args) {
        Jedis conn = new Jedis("localhost");
        conn.select(0);

        String viewdKey = "viewd:1";

        createData(conn, viewdKey);

        print(conn,viewdKey,0,9);
//        removeData(conn, viewdKey);
    }

    private static void removeData(Jedis conn, String viewdKey) {

        System.out.println("当前集合的长度:"+conn.zcard(viewdKey));
        double firstItem = conn.zscore(viewdKey, "item-" + 0);
        long remSize = conn.zremrangeByRank(viewdKey, 0, -2);
        System.out.println("删除了"+remSize);
        System.out.println("当前集合的长度:"+conn.zcard(viewdKey));

    }

    private static void print(Jedis conn, String viewdKey,long start,long end) {
        Set<Tuple> viewds = conn.zrangeWithScores(viewdKey, start, end);
        for (Tuple tuple : viewds) {
            System.out.println("  "+tuple.getElement()+" , score->"+tuple.getScore());
        }
    }

    private static void createData(Jedis conn, String viewdKey) {
        for (int i = 0; i < 10; i++) {

//            long now = System.currentTimeMillis() / 1000;
            String item="item-"+i;
            conn.zadd(viewdKey, i, item);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
